﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace QldtSdh.GUI.DaoTao
{
    using BLL;
    using DAL;
    using DAL.Models;
    using GUI.Utils;
    using QldtSdh.GUI.Common;
    using QldtSdh.Utils;

    public partial class FormLopHocPhan_ThoiKhoaBieu_KetQuaSaoChep : QldtSdh.GUI.Common.BaseDialog
    {
        private bool _DoNotUpdateGuiAfterSelect = false;
        private static FormLopHocPhan_ThoiKhoaBieu_KetQuaSaoChep _Instance = null;

        public static void MinimizeInstance()
        {
            if (_Instance != null && _Instance.IsDisposed == false)
                _Instance.WindowState = FormWindowState.Minimized;
        }

        public static void RestoreInstance()
        {
            if (_Instance != null && _Instance.IsDisposed == false)
                _Instance.WindowState = FormWindowState.Normal;
        }

        private UcLopHocPhan_ThoiKhoaBieu _ParentUC = null;

        public static void ShowKetQua(List<TreeNode> lstKetQua, UcLopHocPhan_ThoiKhoaBieu uc, string msg)
        {
            if (_Instance == null || _Instance.IsDisposed)
                _Instance = new FormLopHocPhan_ThoiKhoaBieu_KetQuaSaoChep(lstKetQua);
            else
                _Instance.NapKetQua(lstKetQua);

            _Instance._ParentUC = uc;
            _Instance.lblHeader.Text = msg;
            _Instance.Show();
        }

        private FormLopHocPhan_ThoiKhoaBieu_KetQuaSaoChep(List<TreeNode> lstKetQua)
        {
            InitializeComponent();

            NapKetQua(lstKetQua);
        }

        private void NapKetQua(List<TreeNode> lstKetQua)
        {
            _DoNotUpdateGuiAfterSelect = true;
            try
            {
                //tree.ImageList = imlStatus;
                //tree.Location = treeLog.Location;
                //tree.Anchor = treeLog.Anchor;
                //tree.Size = treeLog.Size;
                //tree.AfterSelect += treeLog_AfterSelect;
                //this.Controls.Remove(treeLog);
                //this.Controls.Add(treeLog);
                //treeLog = tree;

                treeLog.Nodes.Clear();
                foreach (var node in lstKetQua)
                    treeLog.Nodes.Add(node);
                treeLog.ExpandAll();
                //treeLog.SelectedNode = treeLog.Nodes[0];
            }
            finally
            {
                _DoNotUpdateGuiAfterSelect = false;
            }
            UpdateGuiBySelectedNode();
        }

        private void UpdateGuiBySelectedNode()
        {
            KetQuaSaoChep item = null;
            var node = treeLog.SelectedNode;
            if (node != null && node.Tag != null)
                item = node.Tag as KetQuaSaoChep;
            if (item == null)
            {
                txtInfo.Text = "";
            }
            else
            {
                var tkb = item.MucTKB;
                var buoiHoc = BizThoiKhoaBieu.GetStringOfBuoi(tkb.BuoiHoc);
                var ngayHoc = string.Format("{0} {1} ({2})", buoiHoc, BizThoiKhoaBieu.GetThuTrongTuan(tkb.NgayHoc), tkb.NgayHoc.ToString("dd/MM"));
                var giangVien = string.Format("Giảng viên: {0}", tkb.GiangVien.GiangVienDescTKB);
                var phongHoc = string.Format("Phòng học: {0}", tkb.MaPhongHoc);
                var kq = item._KetQua;
                txtInfo.Text = ngayHoc + "\r\n" + giangVien + "\r\n" + phongHoc + "\r\n" + kq;
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void treeLog_AfterSelect(object sender, TreeViewEventArgs e)
        {
            UpdateGuiBySelectedNode();
        }

        private void FormLopHocPhan_ThoiKhoaBieu_KetQuaSaoChep_FormClosed(object sender, FormClosedEventArgs e)
        {
            _Instance = null;
        }

        private void treeLog_DoubleClick(object sender, EventArgs e)
        {
            if (_ParentUC == null)
                return;
            var node = treeLog.SelectedNode;
            if (node == null)
                return;
            if (node.Tag == null)
                return;
            var ketquasaochep = node.Tag as KetQuaSaoChep;
            if (ketquasaochep == null)
                return;
            var tkb = ketquasaochep.MucTKB;
            if (tkb == null)
                return;
            //_ParentUC.SetCurrentCell(tkb.NgayHoc, tkb.BuoiHoc);
            _ParentUC.HighlightAt(tkb.NgayHoc, tkb.BuoiHoc);
        }

        internal static void CloseIt()
        {
            if (_Instance != null && _Instance.IsDisposed == false)
                _Instance.Close();
        }
    }

    public class KetQuaSaoChep
    {
        public LopHocPhan_ThoiKhoaBieu MucTKB { get; set; }
        public bool ThanhCong { get; set; }
        public string MoTaLoi { get; set; }
        public string _KetQua
        {
            get
            {
                if (ThanhCong)
                    return "Đã sao chép";
                else
                    return MoTaLoi;
            }
        }
    }
}
